package com.program.facesigninsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.program.facesigninsystem.dao.ISignInActivityDao;
import com.program.facesigninsystem.entity.SignInActivity;
import com.program.facesigninsystem.entity.SignInLink;
import com.program.facesigninsystem.entity.SignInRecord;
import com.program.facesigninsystem.iteration.dto.AddSigninLinkParam;
import com.program.facesigninsystem.iteration.entity.SigninlinkEntity;
import com.program.facesigninsystem.iteration.mapper.SigninlinkMapper;
import com.program.facesigninsystem.service.ISignInActivityService;
import com.program.facesigninsystem.util.GetInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Service
public class SignInActivityService implements ISignInActivityService {
    @Autowired
    private ISignInActivityDao iSignInActivityDao;
    @Resource
    SigninlinkMapper signinlinkMapper;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private TransactionDefinition transactionDefinition;

//    @Override
//    public void addNewActivity(String actId, String title, String createTime, String actType, String caption, String cid, String tid,int signStopType) {
//        iSignInActivityDao.addNewActivity(actId, title, createTime, actType, caption, cid, tid,signStopType);
//    }


    @Override
    public void addNewActivity(String actId, String title, String createTime, String actType, String caption, String cid, String tid, int signStopType, int signInType,int actCircle,String originactId,String ccid) {
        iSignInActivityDao.addNewActivity(actId, title, createTime, actType, caption, cid, tid,signStopType,signInType,actCircle,originactId,ccid);
    }

    @Override
    public void addNewLink(String lid, int linkType, String duration, String startTime, String endTime, int status, String actId, double location_x, double location_y, double range) {
        iSignInActivityDao.addNewLink(lid, linkType, duration, startTime, endTime, status, actId, location_x, location_y, range);
    }

    @Override
    public String selectnowlidbyccid(String ccid) {
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String currenttime = currentTime.format(formatter);
        return iSignInActivityDao.selectnowlidbyccid(ccid,currenttime);
    }

    @Override
    public void updateLinkStatus(String lid, int status, String startTime, String endTime) {
        iSignInActivityDao.updateLinkStatus(lid, status, startTime, endTime);
    }

    @Override
    public Object[] selectActivityByTid(String tid, String createTime) {
        return iSignInActivityDao.selectActivityByTid(tid, createTime);
    }

    @Override
    public List<SignInActivity> selectActivityByMonth(String tid, String createTime) {
        return iSignInActivityDao.selectActivityByMonth(tid, createTime);
    }

    @Override
    public Object[] selectLinkByActId(String actId) {
        return iSignInActivityDao.selectLinkByActId(actId);
    }

    @Override
    public Object[] selectLinkByActId(String actId, int pageSize, int pageNum) {
        //PageHelper.startPage(pageSize, pageNum);
        return iSignInActivityDao.selectLinkByActId(actId);
    }

    @Override
    public void updateActivityCircleByID(String actId, int actCircle){
        iSignInActivityDao.updateActivityCircleByID(actId,actCircle);
    };
    @Override
    public void deleteActivityByID(String actId) {
        iSignInActivityDao.deleteActivityByID(actId);
    }

    @Override
    public SignInActivity selectAct(String lid) {
        return iSignInActivityDao.selectAct(lid);
    }

    @Override
    public SignInLink selectLinkByLid(String lid) {
        return iSignInActivityDao.selectLinkByLid(lid);
    }

    @Override
    public List<Map<String, Object>> selectList(String tid) {
        return iSignInActivityDao.selectList(tid);
    }

    @Override
    public Integer isopen(String tid){
        return iSignInActivityDao.selectopen(tid);
    }

    @Override
    public Integer isgroup(String tid){
        return iSignInActivityDao.selectgroup(tid);
    }

    @Override
    public List<Map<String,Object>> selectList1(String tid, LocalDateTime begindate, LocalDateTime enddate){
        List<Map<String, Object>> mapList = iSignInActivityDao.selectList(tid);
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<Map<String, Object>> mapList1=new ArrayList<>();
        try {
            for (Map<String, Object> map : mapList) {
                LocalDateTime date = LocalDateTime.parse(map.get("createtime").toString(), sdf);
                if (begindate.isBefore(date) && enddate.isAfter(date)) {
                    mapList1.add(map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapList1;
    }
    @Override
    public List<SignInLink> selectLinkList(String actid) {
        return iSignInActivityDao.selectLinkList(actid);
    }

    @Override
    public Boolean updateLinkDuration(String lid, int duration) {

        //开启事务
        TransactionStatus status = platformTransactionManager.getTransaction(transactionDefinition);
        try {

            SigninlinkEntity signinlinkEntity = signinlinkMapper.selectOne(new QueryWrapper<SigninlinkEntity>().eq("lid", lid));
            signinlinkEntity.setDuration(String.valueOf(duration));
            if (signinlinkEntity.getStarttime() != null){
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String endTime = simpleDateFormat.format(signinlinkEntity.getStarttime().getTime() +60000*duration);
                signinlinkEntity.setEndtime(simpleDateFormat.parse(endTime));
            }
            if (duration == -1){
                signinlinkEntity.setDuration("∞");
            }
            signinlinkMapper.update(signinlinkEntity, new QueryWrapper<SigninlinkEntity>().eq("lid", lid));

            //提交事务
            platformTransactionManager.commit(status);
            return true;
        } catch (Exception e) {

            //回滚事务
            log.info(e.getMessage());
            platformTransactionManager.rollback(status);
            return false;
        }
    }

    @Override
    public Boolean addOneNewLink(AddSigninLinkParam addSigninLinkParam) {

        SigninlinkEntity signinlinkEntity = new SigninlinkEntity();
        signinlinkEntity.setLid(GetInfoUtil.getId())
                .setLinktype(addSigninLinkParam.getLinktype())
//                .setStarttime()
//                .setEndtime()
                .setDuration(addSigninLinkParam.getDuration())
                .setStatus(0)
                .setActid(addSigninLinkParam.getActid());

        if (addSigninLinkParam.getRangement() != 0){
            signinlinkEntity.setRangement(addSigninLinkParam.getRangement());
        }else {
            signinlinkEntity.setLocationX(0.0)
                    .setLocationY(0.0)
                    .setRangement(0.0);
        }
        return signinlinkMapper.insert(signinlinkEntity) == 1;
    }

    @Override
    public Integer selectSignInType(String decodeCode) {
        return iSignInActivityDao.selectSignInType(decodeCode);
    }

    @Override
    public List<SignInActivity> selectAllList(String tid) {
        List<SignInActivity> activities = iSignInActivityDao.selectAllList(tid);
        for(SignInActivity signInActivity:activities){
            List<SignInLink> signInLinks =iSignInActivityDao.selectByActId(signInActivity.getActId());
            signInActivity.setSignInLinks(signInLinks);
        }
        return activities;
    }

    @Override
    public List<SignInRecord> selectContentBylid(String uid, String lid) {
        return iSignInActivityDao.selectContentBylid(uid, lid);
    }

    @Override
    public List<SignInActivity> findByCCID(String ccid) {
        return iSignInActivityDao.selectActbyccid(ccid);
    }
}
